package cn.edu.neusoft.mall.Dao.impl;

import cn.edu.neusoft.framework.db.JDBCTemplate;
import cn.edu.neusoft.framework.db.RowMapper;
import cn.edu.neusoft.mall.Dao.petDao;
import cn.edu.neusoft.mall.entity.Pet;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class petDaoImpl implements petDao {
    RowMapper<Pet> rowMapper=new RowMapper<Pet>() {
        @Override
        public Pet mapRow(ResultSet rs, int rowNum) throws SQLException {
            Pet pet=new Pet();
            pet.setQueryNumber(rs.getInt("query_number"));
            pet.setName(rs.getNString("name"));
            pet.setBreed(rs.getNString("breed"));
            pet.setGender(rs.getNString("gender"));
            pet.setBirthday(rs.getDate("birthday"));
            pet.setPrice(rs.getDouble("price"));
            pet.setImagePath(rs.getNString("image_path"));
            pet.setImagePath2(rs.getNString("image_path2"));
            pet.setImagePath3(rs.getNString("image_path3"));
            pet.setImagePath4(rs.getNString("image_path4"));
            pet.setSub(String.valueOf(rs.getInt("sub")));
            return pet;
        }
    };
    //首页显示热门宠物
    @Override
    public List<Pet> getpetList() {
        String sql = "select * from dogs order by query_number desc limit 3,4";
        return JDBCTemplate.query(sql,rowMapper,null);
    }
    //首页显示热门宠物点击详情进入单个宠物页
    @Override
    public Pet getdogsde(int query_number) {
        String sql = "select * from dogs where query_number=?";
        return JDBCTemplate.query(sql,rowMapper,query_number).get(0);
    }

    @Override
    public List<Pet> getmoredogspage(int page) {
        String sql = "select * from dogs order by query_number desc limit ?,?";
        return JDBCTemplate.query(sql,rowMapper,(page-1)*8,8);
    }
    @Override
    public List<Pet> getmoredogs() {
        String sql = "select * from dogs order by query_number desc ";
        return JDBCTemplate.query(sql,rowMapper,null);
    }
    @Override
    public Pet getcatsde(int query_number) {
        String sql = "select * from cats where query_number=?";
        return JDBCTemplate.query(sql,rowMapper,query_number).get(0);
    }

    @Override
    public List<Pet> getmorecats() {
        String sql = "select * from cats order by query_number desc ";
        return JDBCTemplate.query(sql,rowMapper,null);
    }
    @Override
    public List<Pet> getmorecatspage(int page) {
        String sql = "select * from cats order by query_number desc limit ?,?";
        return JDBCTemplate.query(sql,rowMapper,(page-1)*8,8);
    }

    RowMapper<Pet> rowMapper2=new RowMapper<Pet>() {
        @Override
        public Pet mapRow(ResultSet rs, int rowNum) throws SQLException {
            Pet pet=new Pet();
            pet.setQueryNumber(rs.getInt("pd.query_number"));
            pet.setName(rs.getNString("pd.name"));
            pet.setBreed(rs.getNString("pd.breed"));
            pet.setGender(rs.getNString("pd.gender"));
            pet.setBirthday(rs.getDate("pd.birthday"));
            pet.setPrice(rs.getDouble("pd.price"));
            pet.setImagePath(rs.getNString("pd.image_path"));
            pet.setImagePath2(rs.getNString("pd.image_path2"));
            pet.setImagePath3(rs.getNString("pd.image_path3"));
            pet.setImagePath4(rs.getNString("pd.image_path4"));
            pet.setSub(String.valueOf(rs.getInt("pd.sub")));
            return pet;
        }
    };
    RowMapper<Pet> rowMapper3=new RowMapper<Pet>() {
        @Override
        public Pet mapRow(ResultSet rs, int rowNum) throws SQLException {
            Pet pet=new Pet();
            pet.setQueryNumber(rs.getInt("pc.query_number"));
            pet.setName(rs.getNString("pc.name"));
            pet.setBreed(rs.getNString("pc.breed"));
            pet.setGender(rs.getNString("pc.gender"));
            pet.setBirthday(rs.getDate("pc.birthday"));
            pet.setPrice(rs.getDouble("pc.price"));
            pet.setImagePath(rs.getNString("pc.image_path"));
            pet.setImagePath2(rs.getNString("pc.image_path2"));
            pet.setImagePath3(rs.getNString("pc.image_path3"));
            pet.setImagePath4(rs.getNString("pc.image_path4"));
            pet.setSub(String.valueOf(rs.getInt("pc.sub")));
            return pet;
        }
    };

    @Override
    public List<Pet> getLovepetdog(int userid) {
        String sql = "select * from userlove as u join pet_store.dogs pd on u.query_number = pd.query_number   where  u.user_id=?";
        return JDBCTemplate.query(sql,rowMapper2,userid);
    }

    @Override
    public List<Pet> getLovepetcat(int userid) {
        String sql = "select * from userlove as u  join pet_store.cats pc on u.query_number = pc.query_number where  u.user_id=?";
        return JDBCTemplate.query(sql,rowMapper3,userid);
    }

    @Override
    public List<Pet> getsubpetcat(int userid) {
        String sql="select * from subscribe as s join pet_store.cats pc on s.pet_id = pc.query_number   where  s.user_id=?";
        return JDBCTemplate.query(sql,rowMapper3,userid);
    }

    @Override
    public List<Pet> getsubpetdog(int userid) {
        String sql="select * from subscribe as s join pet_store.dogs pd on s.pet_id = pd.query_number   where  s.user_id=?";
        return JDBCTemplate.query(sql,rowMapper2,userid);
    }

    @Override
    public int setPetdogsub(String petId) {

        String sql="update dogs set  sub=? where query_number=?";
        return JDBCTemplate.update(sql,1,petId);
    }

    @Override
    public int setPetcatsub(String petId) {
        String sql="update cats set  sub=? where query_number=?";
        return JDBCTemplate.update(sql,1,petId);
    }


}
